import request from '../../../utils/request'
Page({
  data: {
  },

  onLoad: function(options) {
      //接收团队页面传来的数据
    const teamInfoStr = decodeURIComponent(options.teamInfo);
    const institutionInfoStr = decodeURIComponent(options.institutionInfo);
    const teamInfo = JSON.parse(teamInfoStr);
    const institutionInfo = JSON.parse(institutionInfoStr);
    // console.log('institutionInfo', institutionInfo);
    // console.log('Team Info:', teamInfo);
    // console.log('Institution Info:', institutionInfo);
    // 将这些数据存储到页面的 data 中
    this.setData({
        teamInfo: teamInfo,
        institutionInfo: institutionInfo
    });
    let docid = options.id
    this.getdoctordetail(docid)
    .then(docdetail=>{
        this.setData({
            doctor:docdetail
        })
    })
    //获取doctag
    this.getdoctag(docid)
    .then(tagId=>{
        this.setData({
            tagIds:tagId
        })
        this.gettagdetail(tagId)
        .then((tagName)=>{
            this.setData({
                tagName:tagName
            })
        })
    })
  },
//获取医生详细信息
getdoctordetail(docid){
    return new Promise((resolve,reject)=>{
        let url = `system/doctor/${docid}`
        request({
            url:url,
            method: 'GET',
            success(res){
                console.log("doctor:",res.data);
                let docdetail = res.data.data
                resolve(docdetail)
            },fail(error){
                reject(error)
            }
        })
    })
},
//获取doctagid
getdoctag(docid){
    let url = `sysmanagement/doctag/${docid}`
    return new Promise((resolve,reject)=>{
        request({
            url:url,
            method:'GET',
            success(res){
                let tagId = [];
                let tagArray = res.data.data
                tagArray.forEach(item=>{
                    tagId.push(item.tagId)
                })
                // console.log("-----",tagId);
                resolve(tagId)
            },fail(error){
                reject(error)
            }
        })
    })
    
},
//获取标签信息
gettagdetail(tagId){
    let url = `sysmanagement/tag/`
    let tagdetail = []
    let tagName=[]
    const requests = tagId.map(item=>{
        return new Promise((resolve,reject)=>{
            request({
                url:url+item,
                method:'GET',
                success(res){
                    // console.log(res.data.data);
                    tagdetail.push(res.data.data)
                    resolve(tagdetail)
                },fail(error){
                    reject(error)
                }
            })
        })
    })
     // 使用 Promise.all 等待所有请求完成
     return Promise.all(requests).then(() => {
        // console.log("lll", tagdetail);
        // 可以在这里继续处理 tagdetail 数组
        // 例如提取 tagName 等操作
        tagdetail.forEach(item => {
            tagName.push(item.tagName);
        });
        // console.log("lllllll",tagName);
        // 可以将 tagName 也返回，方便后续使用
        return tagName;
    }).catch(error => {
        console.error('请求出错:', error);
        throw error;
    });
},
  loadDoctorInfo: function() {
    // TODO: 从服务器获取医生详细信息
    // wx.request({
    //   url: 'your-api-endpoint',
    //   data: { id: this.data.doctor.id },
    //   success: (res) => {
    //     this.setData({
    //       doctor: res.data
    //     });
    //   }
    // });
  },

  goBack: function() {
    wx.navigateBack({
      delta: 1
    });
  },

  refreshPage: function() {
    this.loadDoctorInfo();
  },

  sendMessage: function() {
    let t = this
    // TODO: 实现发送消息功能
    wx.navigateTo({
      url: '/pages/message/chat/chatdetail/chatdetail',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', 
                                {id: t.data.doctor.id,
                                avatar: t.data.doctor.avator,
                                name: t.data.doctor.name })
      }
    })
  },

  makeCall: function() {
    const doctor = this.data.doctor;
    if (doctor && doctor.tel) {
      wx.makePhoneCall({
        phoneNumber: doctor.tel,
        success: () => {
          console.log('拨打电话成功');
          wx.showToast({
            title: '拨打电话成功',
            icon: 'none'
          });
        },
        fail: (err) => {
          console.error('拨打电话失败', err);
          wx.showToast({
            title: '拨打电话失败',
            icon: 'none'
          });
        }
      });
    } else {
      wx.showToast({
        title: '未获取到医生电话',
        icon: 'none'
      });
    }
  },

  requestService: function() {
    // TODO: 实现申请服务功能
   wx.navigateTo({
     url: '/pages/signrecord/signrecord',
   })
  }
}); 